home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / inherit3.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  4KB  |  141 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: inherit3.c,v 1.3 1997/07/09 13:24:50 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    Filename:     inherit3.c
  35.  *
  36.  *  support program for inherita.c - read docs in that file
  37.  */
  38.  
  39. #ifndef WIN32
  40. #include <unistd.h>        /* for gethostname */
  41. #else
  42. #include "pvmwin.h"
  43. #endif
  44. #include "pvm3.h"
  45.  
  46. main()
  47. {
  48.     char *me = "inherit3";
  49.     int cc, tid, ptid;
  50.     char buf[100], buf2[100];
  51.     char machine[25];
  52.     int mycontext, context_1;
  53.  
  54.     ptid = pvm_parent();
  55.  
  56.     gethostname( machine, 25 );
  57.  
  58.     sprintf( buf, "%s t%x on machine <%s> with context %d.",
  59.             me, pvm_mytid(), machine, pvm_getcontext() );
  60.  
  61.     /* Send message to parent */
  62.     pvm_initsend( PvmDataDefault );
  63.     pvm_pkstr( buf );
  64.     pvm_send( ptid, 1 );
  65.  
  66.     context_1 = pvm_newcontext();                /* create new context */
  67.     mycontext = pvm_setcontext( context_1 );    /* activate new context */
  68.  
  69.     sprintf( buf, "%s t%x on machine <%s> with context %d.",
  70.             me, pvm_mytid(), machine, pvm_getcontext() );
  71.  
  72.     pvm_setcontext( mycontext );
  73.  
  74.     /* Send message to parent */
  75.     pvm_initsend( PvmDataDefault );
  76.     pvm_pkstr( buf );
  77.     pvm_send( ptid, 1 );
  78.     pvm_setcontext( context_1 );
  79.  
  80.     /*
  81.      *  Spawn a child process to confirm that it will inherit the
  82.      *   parent context.
  83.      */
  84.     cc = pvm_spawn( "inherit2", (char **) 0, PvmTaskDefault, "", 1,
  85.             &tid );
  86.     if ( cc != 1 ) {
  87.         printf( "%s: can't start inherit2\n", me );
  88.         pvm_exit();
  89.         exit( 0 );
  90.     }
  91.  
  92.     sprintf( buf,
  93.             "%s: I just spawned inherit2 as tid t%x with context %d.\n",
  94.             me, tid, pvm_getcontext() );
  95.  
  96.     pvm_setcontext( mycontext );
  97.  
  98.     /* Send message to parent */
  99.     pvm_initsend( PvmDataDefault );
  100.     pvm_pkstr( buf );
  101.     pvm_send( ptid, 1 );
  102.  
  103.     pvm_setcontext( context_1 );
  104.  
  105.     /*
  106.      *  wait to receive message from child
  107.      */
  108.     cc = pvm_recv( -1, -1 );
  109.     pvm_bufinfo( cc, (int *) 0, (int *) 0, &tid );
  110.     pvm_upkstr( buf2 );
  111.     sprintf( buf, "%s: t%x %s\n", me, tid, buf2 );
  112.  
  113.     pvm_setcontext( mycontext );
  114.  
  115.     /* Send message to parent */
  116.     pvm_initsend( PvmDataDefault );
  117.     pvm_pkstr( buf );
  118.     pvm_send( ptid, 1 );
  119.  
  120.     sprintf( buf, "%s t%x on machine <%s> with context %d.",
  121.             me, pvm_mytid(), machine, pvm_getcontext() );
  122.  
  123.     /* Send message to parent */
  124.     pvm_initsend( PvmDataDefault );
  125.     pvm_pkstr( buf );
  126.     pvm_send( ptid, 1 );
  127.  
  128.     sprintf( buf,
  129.             "END - sent from %s t%x on machine <%s> with context %d.",
  130.             me, pvm_mytid(), machine, pvm_getcontext() );
  131.  
  132.     /* Send message to parent */
  133.     pvm_initsend( PvmDataDefault );
  134.     pvm_pkstr( buf );
  135.     pvm_send( ptid, 1 );
  136.  
  137.     pvm_exit();
  138.     exit( 0 );
  139. }
  140.  
  141.